package com.example.demo.testdemo;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;
/**
 * <p>
 * 延时消息生产者demo
 * </p>
 *
 * @author Jon
 * @since 2021/7/14 11:16
 */
public class ScheduledMessageConsumer {
    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_delay_order_group");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        consumer.subscribe("topic_delay_order","*");
        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
                for (MessageExt msg : msgs) {
                    System.out.println(
                            ", msgId" + msg.getMsgId()
                                    + ", delayTime" + (System.currentTimeMillis() - msg.getStoreTimestamp()));
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        consumer.start();
        System.out.println("consumer starting...");
    }
 }
